var name = null;//修改信息前姓名
var phone = null;//修改信息前电话
var className = null ; //修改信息前部门
var id = null; //修改信息前id
var department =null ;
var status = null ;
var where = null ;
var brith = null ;
var marry = null ;
var id_car = null ;
var spouse = null ;
var car = null ;
var bz = null ;
var sex = null ;
var daoban = null ;
var both = null;

var theData = new Vue({
    el: "#wrap",
    data: {
        nowPage: 1,   //当前页码
        totalPage: 0,  //总页码
        paginationItems: [   //页面数组
            /*{page:{}}*/  //数据格式
        ],
        tableItems: [       //表格数组
            /*{info:{}}*/  //数据格式
        ],
        setUp_user: {},     //配置的用户
        search_btn_remark: "",    //存放用户搜索内容
        token: token,        //input file 的加密
        time: unixtime       //input file 的加密
    },
    methods: {
        getList: function (nextPage) {   //获取每个分页的数据
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/person/get_user_list.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    nextPage: nextPage,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("请求超时");
                },
                success: function (res) {
                    console.log(res);
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        that.tableItems = res.msg;
                        that.nowPage = nextPage;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        getPagination: function () {    //获取分页数量
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/person/pag_info.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        console.log(res.msg);
                        that.paginationItems = res.msg;
                        that.totalPage = res.total_page;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        deleteUserAuthority: function (index) {   //删除用户信息
            MyConfirm("确定删除?", function () {
                $.ajax({
                    url: "api/person/deleteUserInfo.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        index:index,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                console.log(123);
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
        },


        popupdataUserSubmit: function () {//修改用户信息
            var namm = $("#pop-updata-user-name").val();
            var phon = $("#pop-updata-user-phone").val();
            var clas = $("#pop-updata-user-class").val();
            var depa = $("#pop-updata-user-department").val();
            var stat = $("#pop-updata-user-status").val();
            var wher = $("#pop-updata-user-where").val();
            var brit = $("#pop-updata-user-brith").val();
            var marr = $("#pop-updata-user-marry").val();
            var idca = $("#pop-updata-user-id").val();
            var spou = $("#pop-updata-user-spouse").val();
            var carn = $("#pop-updata-user-car").val();
            var bzzz = $("#pop-updata-user-bz").val();
            var sexx = $('#selectError4 option:selected').val();
            var daob = $('#selectError5 option:selected').val();
            var peop = $('#selectError6 option:selected').val();

            if (name == namm  && phone == phon && className == clas && department ==depa && status == stat && where==wher &&
                brith==brit && marry==marr && id_car==idca && spouse==spou && car==carn && bz==bzzz&&
                sex==sexx && daoban ==daob && both==peop) {
                MyAlert("信息未修改！");
            }
            else if(namm==""||clas==""||depa==""||stat==""||wher==""||brit==""||marr==""||sexx==""||daob==""||peop==""){
                MyAlert("请完善信息！");
            }
            else{ MyConfirm("确定修改?", function (){
                $.ajax({
                    url: "api/person/updataUserAuthority.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        name: namm,
                        phone: phon,
                        classname: clas,
                        id: id,
                        department : depa ,
                        status : stat ,
                        from_where :wher ,
                        brith :brit,
                        marry : marr ,
                        id_car : idca,
                        spouse :spou ,
                        car :carn ,
                        bz : bzzz ,
                        sex :sexx ,
                        daoban : daob ,
                        both : peop,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                console.log(123);
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
            }
        },
        search: function () {      //查询
            var regSpace = /^\s+$/;
            if ($("#search-text").val() && !regSpace.test($("#search-text").val())) {
                this.search_btn_remark = $("#search-text").val();
                this.getPagination();
                this.getList(1);
            } else {
                MyAlert("请输入搜索内容");
            }
        },
        popUpdataUser: function (item) {  //修改人员信息弹窗
            $("#pop-updata-user").modal();
            $("#pop-updata-user-name").val(item.info.name);
            $("#pop-updata-user-phone").val(item.info.phone);
            $("#pop-updata-user-class").val(item.info.className);
            $("#pop-updata-user-department").val(item.info.department);
            $("#pop-updata-user-status").val(item.info.status);
            $("#pop-updata-user-where").val(item.info.from_where);
            $("#pop-updata-user-brith").val(item.info.birth_day);
            $("#pop-updata-user-marry").val(item.info.marry);
            $("#pop-updata-user-building").val(item.info.buildingnum);
            $("#pop-updata-user-room").val(item.info.roomnum);
            $("#pop-updata-user-id").val(item.info.id_card);
            $("#pop-updata-user-spouse").val(item.info.spouse);
            $("#pop-updata-user-car").val(item.info.carnum);
            $("#pop-updata-user-bz").val(item.info.bz);
            $('#selectError4').val(item.info.sex);
            $('#selectError5').val(item.info.daoban);
            $('#selectError6').val(item.info.both_people);
            name = item.info.name;//修改信息前姓名
            phone =item.info.phone;//修改信息前电话
            className = item.info.className ; //修改信息前部门
            id = item.info.id; //修改信息前id
            department =item.info.department ;
            status = item.info.status;
            where =item.info.from_where;
            brith =item.info.birth_day;
            marry = item.info.marry;
            building = item.info.buildingnum ;
            room = item.info.roomnum;
            id_car = item.info.id_card;
            spouse = item.info.spouse;
            car =item.info.carnum ;
            bz = item.info.bz ;
            sex = item.info.sex;
            daoban = item.info.daoban;
            both = item.info.both_people;
        },
        popUploadUser: function () {    //人员excel模板弹窗
            $("#pop-upload").modal();
        },
        popAddUser: function () {      //添加单人员弹窗
            $("#pop-add-user").modal();
        },

        //人员添加时入住  楼号 房号 不为空
        join_room: function(floor_name, room_name) {
            $.ajax({
                url: 'api/person/check_room.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    floor_name: floor_name,
                    room_name: room_name,
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                },
                success: function (res) {
                    console.log("keyi");
                }
            })
        },

        popAddUserSubmit: function () {//添加单人人员信息
            var regPhone = /^1(3|4|5|6|7|8)\d{9}$/;
            var regSpace = /^\s+$/;
            var regNum = /[^\d^\.]+/;
            var regIdnum = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

            var name = $("#pop-add-user-name").val();
            var cla = $("#pop-add-user-class").val();
            var department = $("#pop-add-user-department").val();
            var status = $("#pop-add-user-status").val();
            var where = $("#pop-add-user-where").val();
            var brith = $("#pop-add-user-brith").val();
            var marry = $("#pop-add-user-marry").val();
            var phone = $("#pop-add-user-phone").val();
            var building = $("#pop-add-user-building").val();
            var room = $("#pop-add-user-room").val();
            var id = $("#pop-add-user-id").val();
            var spouse = $("#pop-add-user-spouse").val();
            var car = $("#pop-add-user-car").val();
            var bz = $("#pop-add-user-bz").val();
            var sex = $('#selectError1 option:selected').val();
            var daoban = $('#selectError2 option:selected').val();
            var both = $('#selectError3 option:selected').val();
            console.log(phone);
            if (name != "" && sex != "" && cla!="" && department !="" && status !="" &&where!="" && brith != "" &&marry!="" && daoban!="" &&both != ""){
                if (phone!=""&&!regPhone.test(phone)) {
                    MyAlert("请填写正确的手机号码");
                }
                else if(id!=""&&!regIdnum.test(id)){
                    MyAlert("请填写正确的身份证号码");
                } else if(building!=""&&room==""){
                    MyAlert("请填写房间号！");
                } else if(room!=""&&building==""){
                    MyAlert("请填写楼宇号！");
                } else{
                    if(building!=""&& room!=""){
                        console.log(building,room);
                        this.$options.methods.join_room(building,room);
                    }
                    $.ajax({
                        url: 'api/person/add_user.php',
                        type: 'POST',
                        data: {
                            token: token,
                            time: unixtime,
                            name: name,
                            className: cla,
                            department:department,
                            status:status,
                            where:where,
                            brith:brith,
                            marry:marry,
                            phone: phone,
                            building:building,
                            room:room,
                            id:id,
                            spouse:spouse,
                            car:car,
                            bz:bz,
                            sex:sex,
                            daoban:daoban,
                            both:both
                        },
                        dataType: 'JSON',
                        beforeSend: function () {
                            $("#loading-mask").show();
                        },
                        error: function () {
                            $("#loading-mask").hide();
                        },
                        success: function (res) {
                            $("#loading-mask").hide();
                            console.log(res);
                            if (res.err == 0) {
                                MyAlert(res.msg, function () {
                                    console.log(123);
                                    window.location.reload();
                                });

                            } else {
                                MyAlert(res.msg);
                            }
                        }
                    })
                }
            }
            else {
                MyAlert("请填写必填信息");
            }
        },
        popExcelAddUserAuthority: function () {       //Excel添加补助金弹窗
            $("#pop-excel-add-user-authority").modal();
        },
        popExcelAddGrantsSubmit: function () {

        },
        aaa: function () {
            $("#loading-mask").show();
            // $("#loading").modal();
        },


    }
});

theData.getPagination();   //首次加载总页码
theData.getList(theData.nowPage);  //首次加载首个分页